//
//  ZHShowHUD.m
//  ZH_Helper
//
//  Created by caixiang on 2017/10/19.
//  Copyright © 2017年 蔡翔. All rights reserved.
//

#import "ZHShowHUD.h"
@interface ZHShowHUD()<MBProgressHUDDelegate>
{
    MBProgressHUD *_hud;
}
@end
@implementation ZHShowHUD
- (instancetype)initWithView:(UIView *)view
{
    if (view == nil) {
        return nil;
    }
    self = [super init];
    if (self) {
        _hud = [MBProgressHUD showHUDAddedTo:view animated:NO];
        _hud.delegate  = self;// 设置代理
        _hud.animationType  = MBProgressHUDAnimationZoom; // 默认动画样式
        _hud.removeFromSuperViewOnHide = YES; // 该视图隐藏后则自动从父视图移除掉
    }
    return self;
}

- (void)hide:(BOOL)hide afterDelay:(NSTimeInterval)delay
{
    [_hud hideAnimated:hide afterDelay:delay];
}

- (void)hide
{
    [_hud hideAnimated:YES];
}

- (void)show:(BOOL)show
{
    // 根据属性判断是否要显示文本
    if (_text != nil && _text.length != 0) {
        _hud.label.text = _text;
    }
    // 设置文本字体
    if (_textFont) {
        _hud.label.font = _textFont;
    }
    // 如果设置这个属性,则只显示文本
    if (_showTextOnly == YES && _text != nil && _text.length != 0) {
        _hud.mode = MBProgressHUDModeText;
    }
    // 设置背景色
    if (_backgroundColor) {
        _hud.bezelView.color = _backgroundColor;
    }
    // 文本颜色
    if (_labelColor) {
        _hud.label.textColor = _labelColor;
    }
    // 设置圆角
    if (_cornerRadius) {
        _hud.bezelView.layer.cornerRadius = _cornerRadius;
    }
    // 设置透明度
    if (_opacity) {
        _hud.opacity = _opacity;
    }
    // 自定义view
    if (_customView) {
        _hud.mode = MBProgressHUDModeCustomView;
        _hud.customView = _customView;
    }
    // 边缘留白
    if (_margin > 0) {
        _hud.margin = _margin;
    }
    [_hud showAnimated:show];
}

#pragma mark - HUD代理方法
- (void)hudWasHidden:(MBProgressHUD *)hud
{
    [_hud removeFromSuperview];
    _hud = nil;
}
#pragma mark - 重写setter方法
@synthesize animationStyle = _animationStyle;
- (void)setAnimationStyle:(ZHMBProgressHUDAnimation)animationStyle {
    _animationStyle = animationStyle;
    _hud.animationType = (MBProgressHUDAnimation)_animationStyle;
}

- (ZHMBProgressHUDAnimation)animationStyle {
    return _animationStyle;
}

+ (void)showTextOnly:(NSString *)text
     configParameter:(void(^)(ZHShowHUD *config))configShowHUDBlock
            duration:(NSTimeInterval)sec
              inView:(UIView *)view
{
    ZHShowHUD *hud  = [[ZHShowHUD alloc] initWithView:view];
    hud.text = text;
    hud.showTextOnly = YES;
    hud.margin = 10.f;
    !configShowHUDBlock?:configShowHUDBlock(hud);
    [hud show:YES];
    [hud hide:YES afterDelay:sec];
}

+ (void)showText:(NSString *)text
 configParameter:(void(^)(ZHShowHUD *config))configShowHUDBlock
        duration:(NSTimeInterval)sec
          inView:(UIView *)view
{
    ZHShowHUD *hud  = [[ZHShowHUD alloc] initWithView:view];
    hud.text  = text;
    hud.margin = 10.f;
    !configShowHUDBlock?:configShowHUDBlock(hud);
    [hud show:YES];
    [hud hide:YES afterDelay:sec];
}


+ (void)showCustomView:(UIView *(^)(void))configShowHUDCustomViewBlock
       configParameter:(void(^)(ZHShowHUD *config))configShowHUDBlock
              duration:(NSTimeInterval)sec
                inView:(UIView *)view
{
    ZHShowHUD *hud  = [[ZHShowHUD alloc] initWithView:view];
    hud.margin  = 10.f;
    // 配置额外的参数
    !configShowHUDBlock?:configShowHUDBlock(hud);
    // 自定义View
    if (configShowHUDCustomViewBlock) {
        hud.customView  = configShowHUDCustomViewBlock();
    }
    // 显示
    [hud show:YES];
    [hud hide:YES afterDelay:sec];
}


+ (instancetype)showTextOnly:(NSString *)text
             configParameter:(void(^)(ZHShowHUD *config))configShowHUDBlock
                      inView:(UIView *)view
{
    ZHShowHUD *hud  = [[ZHShowHUD alloc] initWithView:view];
    hud.text = text;
    hud.showTextOnly = YES;
    hud.margin = 10.f;
    !configShowHUDBlock?:configShowHUDBlock(hud);
    // 显示
    [hud show:YES];
    return hud;
}

+ (instancetype)showText:(NSString *)text
         configParameter:(void(^)(ZHShowHUD *config))configShowHUDBlock
                  inView:(UIView *)view
{
    ZHShowHUD *hud  = [[ZHShowHUD alloc] initWithView:view];
    hud.text = text;
    hud.margin = 10.f;
    !configShowHUDBlock?:configShowHUDBlock(hud);
    // 显示
    [hud show:YES];
    return hud;
}

+ (instancetype)showCustomView:(UIView *(^)(void))configShowHUDCustomViewBlock
               configParameter:(void(^)(ZHShowHUD *config))configShowHUDBlock
                        inView:(UIView *)view
{
    ZHShowHUD *hud  = [[ZHShowHUD alloc] initWithView:view];
    // 配置额外的参数
    !configShowHUDBlock?:configShowHUDBlock(hud);
    // 自定义View
    if (configShowHUDCustomViewBlock) {
        hud.customView  = configShowHUDCustomViewBlock();
    }
    // 显示
    [hud show:YES];
    return hud;
}

- (void)dealloc {
}

@end
